<template>
<div class="app-container">
    <loading v-show="loading"></loading>
    <el-card class="box-card" style="margin-bottom:1%;">
      <div slot="header" class="clearfix">
        <span>考核周期基本信息</span>
        <div class="btn-box">
        <el-button
              size="mini"
              type="primary"
              @click="$router.back(-1)" style="margin-right:20px;">返 回</el-button>
        <el-button
              size="mini"
              v-if="partyMemberInfoList.length !== 0"
              type="primary"
              icon="el-icon-plus"
              @click="saveReport" style="margin-right:20px">提交</el-button>
        </div>
      </div>
      <el-table
        :data="partyMemberInfoList"
        border
        ref="tableData"
        style="width: 100%;margin-bottom:1%;"
        >
        <el-table-column
          label="党员姓名"
        >
          <template slot-scope="scope">
              {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column
          label="满意度评测"
        >
          <template slot-scope="scope">
            <el-radio v-for="(item,index) in scope.row.dyMydcpDicList" v-model="dyMydcpDetailArray[scope.$index].score" @change="setValue(scope.$index,item.score,item.description)" :key="item.mydcpDicKey" :label="item.score" border>{{item.description+item.score}}</el-radio>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
 </div>
</template>
<script>
import Loading from '@/components/loading/loading'
import { getSelfReported, savaReportData } from '@/api/partyMember/assessment'
export default {
  components: {
    Loading
  },
  data() {
    return {
      searchData: {
        parAssessmentKey: ''
      },
      loading: false,
      dyMydcpDetailArray: [],
      saveData: {
        parAssessmentKey: '',
        data: {}
      },
      dyMydcpDicList: [],
      partyMemberInfoList: [],
      validateFlag: false
    }
  },
  created() {
    this.loading = true
    this.searchData.parAssessmentKey = this.$route.query.parAssessmentKey
    this.getgetSelfReportedList()
  },
  methods: {
    setValue(index, value, text) {
      this.dyMydcpDetailArray[index].description = text + '(' + value + ')'
    },
    getgetSelfReportedList() {
      getSelfReported(this.searchData).then(response => {
        this.dyMydcpDicList = response.data.dyMydcpDicList
        this.partyMemberInfoList = response.data.partyMemberInfoList
        this.partyMemberInfoList.forEach(element => {
          this.dyMydcpDetailArray.push({
            partyOrgKey: element.partyMemberInfoKey, // 被测评党员key
            description: '', // 满意度名称(分值)
            score: ''
          }
          )
          element.dyMydcpDicList = this.dyMydcpDicList
        })
        console.log(this.partyMemberInfoList)
        this.loading = false
      }).catch(error => {
        this.loading = false
        console.log(error)
      })
    },
    saveReport() {
      for (var index in this.dyMydcpDetailArray) {
        if (this.dyMydcpDetailArray[index].description) {
          this.validateFlag = true
        } else {
          this.validateFlag = false
          this.$message({
            showClose: true,
            message: '有未满意度评议的党员',
            type: 'warning'
          })
          break
        }
      }
      if (this.validateFlag) {
        this.loading = true
        this.saveData.parAssessmentKey = this.$route.query.parAssessmentKey
        this.saveData.data.dyMydcpDetailArray = this.dyMydcpDetailArray
        this.saveData.data = JSON.stringify(this.saveData.data)
        savaReportData(this.saveData).then(response => {
          this.loading = false
          this.$router.push({
            path: 'partyMemberSatisfactionAssessment',
            name: '民主评议'
          })
        }).catch(error => {
          this.saveData.data.dyMydcpDetailArray = {}
          this.loading = false
          console.log(error)
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.el-form-item{margin-bottom:0;}
</style>
